package com.topit.show.wxapi.common;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.bike.util.Snippet;
import com.bike.util.TimeManager;
import com.topit.show.core.util.gson.GsonUtils;
import com.topit.show.wxapi.vo.AuthUser;

@Service
public class AuthorizeService {

	private static final Logger log = LoggerFactory.getLogger(AuthorizeService.class);

	 
	static Map<String, Integer>  openIdTime=new HashMap<>();

	/**
	 * 页面获取用户的openid
	 * 
	 * @param appid
	 * @param appsecret
	 * @param code
	 *            用户打开微信授权也页面后返回的code参数
	 * @return
	 */
	public String getOpenId(String appid, String appsecret, String code) {
		// 获取openId的url
		String openId = null;
		String url = WeixinApiUrl.get_openId_url.replace("APPID", appid).replace("SECRET", appsecret).replace("CODE", code);
		String response;
		try {
			response = WeixinApiUtil.httpRequest(appid, url, "GET", null);
			AuthUser authUser = GsonUtils.fromJson(response, AuthUser.class);
			openId = authUser.getOpenid();
		} catch (Exception e) {
			log.error("静默授权获取openid失败，appid={}， 错误原因={}", appid, e.getMessage());
		}
		return openId;
	}

	/**
	 *
	 * @param appid
	 * @param redirectUri
	 * @return
	 */
	public String getOauthBaseUrl(String appid, String shopGzh, String redirectUri) {
		try {
			String url = WeixinApiUrl.get_code_url.replace("APPID", appid).replace("REDIRECT_URI", URLEncoder.encode(redirectUri, "UTF-8")).replace("CODE", "code").replace("SCOPE", "snsapi_base").replace("STATE", shopGzh);
			return url;
		} catch (UnsupportedEncodingException e) {
			log.error("获取认证地址异常", e);
		}

		return WxConstants.SERVER_BASE_PATH + "/error";
	}
	
	
	//发送模板消息
	public static String pushInfo(String openId,String first,String keyword1,String keyword2,String keyword3,String remark) {
			String token=WeixinApiUtil.getAccessToken(WxConstants.APPID, WxConstants.APPSECRET).getAccess_token();
			String url = WeixinApiUrl.template_msg.replace("ACCESS_TOKEN", token);
			Map<String, Object> templateInfo=new HashMap<>();
			//templateInfo.put("touser", "o4DtY08pqC6h3U-RBLNUTbFGyz2M");
			templateInfo.put("touser",openId);
			templateInfo.put("template_id", "rTLIWHpLsM40lPOn5mEoJ1DMEpyhjuMVWjlSj_JTIDM");
			templateInfo.put("url", "http://pk.shomr.com/pkw/bike/index");
			templateInfo.put("topcolor", "#0000FF"); 
			Map<String, Object> data=new HashMap<>();
			
			data.put("first", getParamMap(first,"#0000FF"));
			data.put("keyword1", getParamMap(keyword1,"#0000FF"));//总分数
			data.put("keyword2", getParamMap(keyword2,"#0000FF"));//班级排名
			data.put("keyword3", getParamMap(keyword3,"#0000FF"));//年级排名
			data.put("keyword4", getParamMap(TimeManager.TimenintyToString(System.currentTimeMillis()),"#0000FF"));//时间
			data.put("remark", getParamMap(remark,"#0000FF"));
			
			templateInfo.put("data", data);
			
			JSONObject result=Snippet.httpsRequest(url, "POST", JSONObject.toJSONString(templateInfo));
			
		   return result.getString("errcode");
	}
	//发送模板消息
	public static String pushNotice(String url2,String openId,String first,String keyword1,String keyword2,String remark) {
		String token=WeixinApiUtil.getAccessToken(WxConstants.APPID, WxConstants.APPSECRET).getAccess_token();
		String url = WeixinApiUrl.template_msg.replace("ACCESS_TOKEN", token);
		Map<String, Object> templateInfo=new HashMap<>();
		//templateInfo.put("touser", "o4DtY08pqC6h3U-RBLNUTbFGyz2M");
		templateInfo.put("touser",openId);
		templateInfo.put("template_id", "uVf4HSsmTWgh4SlxrNE1vZgQxcicO-Py0supeXZluEQ");
		templateInfo.put("url", url2);
		templateInfo.put("topcolor", "#0000FF"); 
		Map<String, Object> data=new HashMap<>();
		
		data.put("first", getParamMap(first,"#0000FF"));
		data.put("keyword1", getParamMap(keyword1,"#0000FF"));//科目
		data.put("keyword2", getParamMap(keyword2,"#0000FF"));//年级
		data.put("keyword3", getParamMap(TimeManager.TimenintyToString(System.currentTimeMillis()),"#0000FF"));//提交时间
		data.put("remark", getParamMap(remark,"#0000FF"));
		
		templateInfo.put("data", data);
		
		JSONObject result=Snippet.httpsRequest(url, "POST", JSONObject.toJSONString(templateInfo));
		
		return result.getString("errcode");
	}
	 
	
		public static Map<String, Object> getParamMap(String value,String color){
			Map<String, Object> first=new HashMap<>();
			first.put("value", value);
			first.put("color", color);
			return first;
		}
		
		 
}
